/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateEditSnippet.java
 *
 * Created on 04.06.2011, 03:31:29
 */

package de.realriu.snipzel.gui;

import com.uwyn.jhighlight.renderer.XhtmlRendererFactory;
import de.realriu.riulib.gui.imagelist.AbstractImageList.Alignment;
import de.realriu.riulib.gui.imagelist.ImageListAdapter;
import de.realriu.riulib.gui.imagelist.ReferenzImageList;
import de.realriu.snipzel.control.Controller;
import de.realriu.snipzel.model.CodeSnippet;
import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author riu
 */
public class CreateEditSnippet extends javax.swing.JDialog {
    private Controller control;
    private ReferenzImageList<String> lang_list;
    private boolean newSnippet;
    private String selectedLang = CodeSnippet.UNKNOWN_LANGUAGE_STRING;
    private boolean doSave = false;
    private CodeSnippet createdSnippet;

    /** Creates new form CreateEditSnippet */
    public CreateEditSnippet(java.awt.Frame parent, boolean modal, Controller c) {
        super(parent, modal);
        this.control = c;
        initComponents();
        lang_list = (ReferenzImageList<String>) il_lang;
        newSnippet = c.getCurrentSnippet() == null;

        lang_list.setMargin(5, 5, 5, 5);
        lang_list.setPadding(5, 5, 5, 10);
        lang_list.setTitleColor(Color.BLACK);


        for(String s : (Set<String>)XhtmlRendererFactory.getSupportedTypes()){
            lang_list.addImage((Image)control.getImageForLang(s), s, s);
        }

        lang_list.addReferenceImageListListener(new ImageListAdapter<String>() {

            @Override
            public void imageSelected(int pos) {
                if(pos > -1){
                    selectedLang = lang_list.getReferenz(pos);
                }
            }

        });

        if(!newSnippet){
            initEditorMode();
        }
    }

    public boolean isNewSnippet() {
        return newSnippet;
    }

    public boolean isDoSave() {
        return doSave;
    }

    private void initEditorMode(){
        //Sprache
        lang_list.selectImage(lang_list.indexOf(control.getCurrentSnippet().getLanguage()));

        //Title
        tx_title.setText(control.getCurrentSnippet().getTitle());

        //Code
        ep_code.setText(control.getCurrentSnippet().getCode());

        //Tags
        tx_tags.setText(control.getCurrentSnippet().getTags());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        il_lang = new ReferenzImageList<String>(Alignment.Horizontal, false);
        jScrollPane1 = new javax.swing.JScrollPane();
        ep_code = new javax.swing.JEditorPane();
        jPanel3 = new javax.swing.JPanel();
        tx_title = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        tx_tags = new javax.swing.JTextField();
        btn_save = new javax.swing.JButton();
        btn_abort = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Language"));

        il_lang.setBackground(new java.awt.Color(255, 255, 255));

        org.jdesktop.layout.GroupLayout il_langLayout = new org.jdesktop.layout.GroupLayout(il_lang);
        il_lang.setLayout(il_langLayout);
        il_langLayout.setHorizontalGroup(
            il_langLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 620, Short.MAX_VALUE)
        );
        il_langLayout.setVerticalGroup(
            il_langLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 120, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(il_lang, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(il_lang, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        ep_code.setBackground(null);
        ep_code.setBorder(javax.swing.BorderFactory.createTitledBorder("Code Snippet"));
        jScrollPane1.setViewportView(ep_code);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Title"));

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, tx_title, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(tx_title, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Tags"));

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, tx_tags, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(tx_tags, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        btn_save.setText("Save");
        btn_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_saveActionPerformed(evt);
            }
        });

        btn_abort.setText("Abort");
        btn_abort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_abortActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(btn_save)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 444, Short.MAX_VALUE)
                .add(btn_abort)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btn_save)
                    .add(btn_abort))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_saveActionPerformed
        doSave = true;

        if(newSnippet){
            try {
                createdSnippet = new CodeSnippet(control.getNewSnippetID(), tx_title.getText(), selectedLang, ep_code.getText(), tx_tags.getText());
                control.saveSnippet(createdSnippet);
            } catch (IOException ex) {
                ex.printStackTrace();
                System.exit(1);
            }
        }else{
            control.getCurrentSnippet().setTitle(tx_title.getText());
            control.getCurrentSnippet().setCode(ep_code.getText());
            control.getCurrentSnippet().setLanguage(selectedLang);
            control.getCurrentSnippet().setTags(tx_tags.getText());

            try {
                control.updateSnippet(control.getCurrentSnippet());
            } catch (IOException ex) {
                ex.printStackTrace();
                System.exit(1);
            }
        }

        dispose();
    }//GEN-LAST:event_btn_saveActionPerformed

    private void btn_abortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_abortActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_abortActionPerformed

    public CodeSnippet getCreatedSnippet() {
        return createdSnippet;
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_abort;
    private javax.swing.JButton btn_save;
    javax.swing.JEditorPane ep_code;
    javax.swing.JPanel il_lang;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    javax.swing.JTextField tx_tags;
    javax.swing.JTextField tx_title;
    // End of variables declaration//GEN-END:variables

}
